*             ======================================================
* Emperical Analysis for Stepping Stone or Stumbling Block? Green Factory Pilot Policy and Corporate Financing Constraints
*             ======================================================
**#base setting
cd "D:\paper\financing_constraints"   

. clear

. global root= "D:\论文\financing_constraints"       
. global Dofiles= "$root\Dofiles"         
. global Rawdata= "$root\Rawdata"
. global Working_data= "$root\Workingdata"
. global Tables= "$root\Tables"
. global Figures= "$root\Figures"
. global SomeDeatils= "$root\SomeDeatils"
. global Blog= "$root\Blog"

. cap !mkdir "$Raw_data"              
. cap  mkdir "$Working_data"
. cap  mkdir "$Tables"          
. cap  mkdir "$Figures"
. cap  mkdir "$Dofiles"          
. cap  mkdir "$root\Paper"
. cap  mkdir "$root\References"
. cap  mkdir "$Rawdata"
. cap  mkdir "$Workingdata"
. cap  mkdir "$Figures" 
. cap  mkdir "$Blog" 

use "$root\Workingdata\basic.dta",replace

sort id year
set scheme stsj 
global X="Size Lev ROA1 SOE FirmAge Top1 Indep TobinQ1"
global Z="ListAge"/*PE3 PCF3 PB NetProfit  INST BM1*/
foreach var in $X{
	drop if missing(`var')
}
drop if missing(did)
drop if missing(FC)&missing(KZ)
{
	program define myprogram
cd "D:\paper\financing_constraints"   

. clear

. global root= "D:\paper\financing_constraints"       
. global Dofiles= "$root\Dofiles"         
. global Rawdata= "$root\Rawdata"
. global Working_data= "$root\Workingdata"
. global Tables= "$root\Tables"
. global Figures= "$root\Figures"
. global SomeDeatils= "$root\SomeDeatils"
. global Blog= "$root\Blog"

. cap !mkdir "$Raw_data"              
. cap  mkdir "$Working_data"
. cap  mkdir "$Tables"          
. cap  mkdir "$Figures"
. cap  mkdir "$Dofiles"          
. cap  mkdir "$root\Paper"
. cap  mkdir "$root\References"
. cap  mkdir "$Rawdata"
. cap  mkdir "$Workingdata"
. cap  mkdir "$Figures" 
. cap  mkdir "$Blog" 

use "$root\Workingdata\basic.dta",replace

sort id year
set scheme stsj 
global X="Size Lev ROA1 SOE FirmAge Top1 Indep TobinQ1"
global Z="ListAge"/*PE3 PCF3 PB NetProfit  INST BM1*/
foreach var in $X{
	drop if missing(`var')
}
drop if missing(did)
drop if missing(FC)&missing(KZ)
	end
	}



**#descriptive statistics
{
logout, save ("descriptive statistics") word replace: tabstat FC KZ did $X ,s(N mean sd min median max) c(s)
}

**#baseline regression
{
local n=1
foreach var of varlist FC KZ {
	reghdfe `var'  did, absorb(year id) vce(cluster city)
    estadd local FirmFE "YES"
    estadd local YearFE "YES"
    estimates store BASE`n'
    local n=`n'+1
}
local n=5
foreach var of varlist FC KZ {
	reghdfe `var'  did $X, absorb(year id) vce(cluster id)
    estadd local FirmFE "YES"
    estadd local YearFE "YES"
    estimates store BASE`n'
    local n=`n'+1
}

esttab BASE*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
esttab BASE* using $root\Tables\baseline1.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))
}

**#robust check
**##Parallel trend test

foreach var of varlist FC KZ  {
	reghdfe `var' xh* $X ,a(year id) vce(cl id)
coefplot, baselevels ///
keep(xh*) ///
vertical ///
 yline(0) ///
xline(4, lwidth(vthin) lpattern(shortdash) ) ///
ylabel(,labsize(*0.75)) xlabel(,labsize(*0.75)) ///
ytitle("Dynamic effect of policy", size(small)) ///
xtitle("Relative policy timing", size(small)) ///
addplot(line @b @at) ///
ciopts(lpattern(dash) recast(rcap) msize(medium)) ///
scheme(stsj) ///
levels(90) ///
title("`var'") ///
coeflabels(xh1="-4" xh2="-3" xh3="-2" xh5="0" xh6="1" xh7="2" xh8="3" xh9="4")
graph save $root\Parallel`var'.gph, replace 
graph export $root\Parallel`var'.png, replace 
}

graph combine ParallelFC.gph ParallelKZ.gph , graphregion(color(white)) plotregion(color(white))  ysize(1) xsize(3)
graph export  $root\Figures\Parallel1.png, width(2400) replace



**##Placebo Test
**placebo1
local n=5
foreach var of varlist FC KZ {
	duplicates drop id year, force
	xtset id year
	didplacebo BASE`n', treatvar(did) pbou rep(500) seed(500) 
	graph save $root\placebo`n'.gph, replace 
	graph export $root\placebo`n'.png, replace
	local n=`n'+1
}
 graph combine placebo5.gph placebo6.gph , graphregion(color(white)) plotregion(color(white)) ysize(1) xsize(2)
graph export  $root\Figures\placebo1.png, width(2400) replace



**##Controlling for Policy Interference
local n=1
foreach var of varlist lowcarbon carbontop greenfin carbontr ne{
	reghdfe FC did $X `var' ,a(year id) vce(cl id)
	estimate store POFC`n'
	local n=`n'+1
	reghdfe KZ did $X `var' ,a(year id) vce(cl id)
	estimate store POFC`n'
	local n=`n'+1
}
esttab POFC*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %12.0fc))
esttab POFC* using $root\Tables\POFC.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %12.0fc))



**##PSM-DID

{
local n=1
foreach var of varlist FC KZ{
	use "$root\Workingdata\basic.dta",replace

	sort id year
	set scheme stsj 
	global X="Size Lev ROA1 SOE FirmAge Top1 Indep TobinQ1"
	global Z="PE3 PCF3 PB NetProfit ListAge INST BM1"
	***
	egen `var'mean=mean(`var')
	gen treat=0
	replace treat=1 if `var'>`var'mean
	***One-to-Three Nearest Neighbor Matching
	psmatch2 treat $X  , outcome(`var') neighbor(3) ate ties logit common
	pstest $X ,both graph
	reghdfe `var' did $X  if _weight!=., a(id year) cluster(id)
	estimate store PSM`n'
	local n=`n'+1
	***Caliper Matching
	sum _pscore
	dis 0.25 * r(sd)
	psmatch2 treat $X , outcome(`var') n(3) cal(0.01) ate ties logit common 
	pstest $X ,both graph
	reghdfe `var' did  $X  if _weight!=., a(id year) cluster(id)
	estimate store PSM`n'
	local n=`n'+1
	***Radius Matching
	psmatch2 treat $X, outcome(`var') radius cal(0.01) ate ties logit common 
	pstest $X ,both graph
	reghdfe `var' did  $X  if _weight!=., a(id year) cluster(id)
	estimate store PSM`n'
	local n=`n'+1
	***Kernel Matching
	psmatch2 treat $X, outcome(`var') kernel ate ties logit common 
	pstest $X ,both graph
	reghdfe `var' did  $X  if _weight!=., a(id year) cluster(id)
	estimate store PSM`n'
	local n=`n'+1
	***Local Linear Regression
	psmatch2 treat $X, outcome(`var') llr ate ties logit common 
	pstest $X ,both graph
	reghdfe `var' did  $X  if _weight!=., a(id year) cluster(id)
	estimate store PSM`n'
	local n=`n'+1

	esttab PSM`n'*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
	esttab PSM`n'* using $root\Tables\PSM`var'.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))

}
}

**##Endogeneity Issues: Treatment Effects Model
{
local n=1
foreach var of varlist FC KZ {
	etregress `var' did, treat(did=$X $Z ) vce(cl id)
	estimates store ET`n'
	local n=`n'+1
	etregress_fixedrho `var' did, treat(did=$X $Z ) vce(cl id) rho(0.5) 
	estimates store ET`n'
	local n=`n'+1
}
esttab ET*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE lambda r2_a  N, fmt(%3s %6.4f %6.4f %9.2f)) 
esttab ET* using $root\Tables\ET.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE lambda r2_a  N, fmt(%3s %6.4f %6.4f %9.2f)) 
}
foreach var of varlist $X{
	gen `var'cross=`var'##i.year
}


**##Robust Estimation of Heterogeneous Treatment Effects

**###Stacked did
xtset id year
stackdid FC did $X , tr(did) group(id) w(-6 6) absorb(id year) cluster(id)
estimate store STA1
myprogram
xtset id year
stackdid KZ did $X , tr(did) group(id) w(-6 6) absorb(id year) cluster(id)
estimate store STA2
esttab STA*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s(FirmFE YearFE r2_a N , fmt(%3s %6.4f %6.4f %9.2f)) keep(did $X)
esttab STA* using $root\Tables\STA.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s(FirmFE YearFE r2_a N, fmt(%3s %6.4f %6.4f %9.2f)) keep(did $X)

**###DID multiplegt
did_multiplegt_dyn FC id year did , cluster(id) effects(6) placebo(6) controls($X)   trends_nonparam(year)  by_path(2)
estimate store PLE1
did_multiplegt_dyn KZ id year did , cluster(id) effects(6) placebo(6) controls($X)   trends_nonparam(year)  by_path(2)
estimate store PLE2
esttab PLE*, replace   compress nogap star(* 0.1 ** 0.05 *** 0.01) s(FirmFE YearFE Effect_ℓ N_switchers_effect_ℓ se_effect_ℓ p_equality_effects N , fmt(%3s %6.4f %6.4f %9.2f)) 
esttab PLE* using $root\Tables\PLE.rtf,replace   compress nogap star(* 0.1 ** 0.05 *** 0.01)  s(FirmFE YearFE Effect_ℓ N_switchers_effect_ℓ se_effect_ℓ p_equality_effects N, fmt(%3s %6.4f %6.4f %9.2f)) 





**#Heterogeneity Analysis
**##Heterogeneity in Industry Pollution Characteristics
foreach var in  重污染分组2 重污染分组3{
	reghdfe FC did $X if `var'==0
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store POLLINN`n'
	local n=`n'+1
	reghdfe FC did $X if `var'==1
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store POLLINN`n'
	local n=`n'+1
}
esttab POLLINN*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))
esttab POLLINN* using $root\Tables\POLLINN.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))


**##Heterogeneity in Total Factor Productivity
egen TFPmean=mean(TFP_OLS)
gen tfp=0
replace tfp=1 if TFPmean<TFP_OLS
local n=1
foreach var of varlist FC KZ{
	reghdfe `var' did $X if tfp==0,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store TFP`n'
	local n=`n'+1
	reghdfe `var' did $X if tfp==1,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store TFP`n'
	local n=`n'+1
}

esttab TFP*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))
esttab TFP* using $root\Tables\TFP.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))

**##Heterogeneity in Firm Market Value
egen valuemean=mean(市值A)
gen value=0
replace value=1 if valuemean<市值B
local n=1
foreach var of varlist FC KZ{
	reghdfe `var' did $X if value==0,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store VAL`n'
	local n=`n'+1
	reghdfe `var' did $X if value==1,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store VAL`n'
	local n=`n'+1
}

esttab VAL*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
esttab VAL* using $root\Tables\VALU.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))

**##Heterogeneity in ESG Scores
egen esgmean=mean(ESG)
gen esg=0
replace esg=1 if esgmean<ESG
local n=1
foreach var of varlist FC KZ{
	reghdfe `var' did $X if esg==0,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store ESG`n'
	local n=`n'+1
	reghdfe `var' did $X if esg==1,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store ESG`n'
	local n=`n'+1
}

esttab ESG*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
esttab ESG* using $root\Tables\ESG.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))

**##Heterogeneity in Industrial Solid Waste Generation
egen indpmean=mean(工业固废物产生量)
gen indp=0
replace indp=1 if indpmean<工业固废物产生量
local n=1
foreach var of varlist FC KZ{
	reghdfe `var' did $X if indp==0,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store INDP`n'
	local n=`n'+1
	reghdfe `var' did $X if indp==1,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store INDP`n'
	local n=`n'+1
}

esttab INDP*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
esttab INDP* using $root\Tables\INDP.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))

**#Potential Mechanism
foreach var of varlist lngreeninv iso disclosure  {
	reghdfe  `var' did $X ,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store MECCXC`n'
	local n=`n'+1
}
esttab MECCXC*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
esttab MECCXC* using $root\Tables\MECC_ability.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))

foreach var of varlist envirhon puconcern{
	reghdfe  `var' did $X ,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store MECCCUCC`n'
	local n=`n'+1
}
esttab MECCCUCC*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
esttab MECCCUCC* using $root\Tables\MECC_fame.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))

foreach var of varlist 绿色投资者净值比例 绿色投资者个数 netcredit{
	reghdfe  `var' did $X ,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store MECCCCCF`n'
	local n=`n'+1
}
esttab MECCCCCF*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
esttab MECCCCCF* using $root\Tables\MECC_invest.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))

foreach var of varlist  envirw   greenkn {
	reghdfe  `var' did $X ,a(year id) vce(cl id)
	estadd local FirmFE "YES"
	estadd local YearFE "YES"
	estimates store MECCCCCC`n'
	local n=`n'+1
}
esttab MECCCCCC*, replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01) order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.2f))
esttab MECCCCCC* using $root\Tables\MECC_review.rtf,replace b(3) se(3)  compress nogap star(* 0.1 ** 0.05 *** 0.01)  order(did) s( FirmFE YearFE r2_a  N, fmt(%3s %6.4f %6.4f %9.0f))


**#Extended Analysis: Who Benefits More from the Green Factory Policy?
{
	cd "D:\paper\financing_constraints"   

. clear

. global root= "D:\paper\financing_constraints"       
. global Dofiles= "$root\Dofiles"         
. global Rawdata= "$root\Rawdata"
. global Working_data= "$root\Workingdata"
. global Tables= "$root\Tables"
. global Figures= "$root\Figures"
. global SomeDeatils= "$root\SomeDeatils"
. global Blog= "$root\Blog"

. cap !mkdir "$Raw_data"              
. cap  mkdir "$Working_data"
. cap  mkdir "$Tables"          
. cap  mkdir "$Figures"
. cap  mkdir "$Dofiles"          
. cap  mkdir "$root\Paper"
. cap  mkdir "$root\References"
. cap  mkdir "$Rawdata"
. cap  mkdir "$Workingdata"
. cap  mkdir "$Figures" 
. cap  mkdir "$Blog" 

use "$root\Workingdata\basic.dta",replace

sort id year
set scheme stsj 
global X="Size Lev ROA1 SOE FirmAge Top1 Indep TobinQ1"
global Z="ListAge"/*PE3 PCF3 PB NetProfit  INST BM1*/

}

**##figure
set scheme stsj 
global XX=" lngreeninv envirw envirhon envirill iso greenkn "
 dgraph $X, by(type1) echo
 }
**##MTE 
mtefe FC did lngreeninv envirw envirhon envirill iso greenkn  (type1=$X ), vce(cluster id)  /*Parameter standardization local instrumental variable estimation*/
estimates store MTE1

mtefe KZ did lngreeninv envirw envirhon envirill iso greenkn (type1=$X ) , vce(cluster id) /*Parameter standardization local instrumental variable estimation*/
estimates store MTE2

mtefeplot MTE1, late memory /*Comparison of MTE results with local average treatment effect weights*/
mtefeplot MTE2, late memory /*Comparison of MTE results with local average treatment effect weights*/

esttab MTE*, replace b(3) se(3) s(N  p_U p_X, fmt(%3s %6.4f %6.4f %9.2f)) compress nogap star(* 0.1 ** 0.05 *** 0.01)  
esttab MTE* using $root\Tables\MTE1.rtf,replace b(3) se(3) s(N p_U p_X, fmt(%3s %6.4f %6.4f %9.2f)) compress nogap star(* 0.1 ** 0.05 *** 0.01) order(type1)

